package org.ns.longhttp;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

public class ThreanName implements ThreadFactory {

    private AtomicLong id = new AtomicLong();
    private String name;
    private boolean daemon = false;
    private ThreadGroup threadGroup;

    public ThreanName(String name) {
       this(name, false);
    }

    public ThreanName(String name, boolean daemon) {
        this.name = name;
        this.daemon = daemon;
        this.threadGroup = new ThreadGroup("g_"+name);
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(threadGroup, r, name + "-" + id.incrementAndGet());
        thread.setDaemon(daemon);
        return thread;
    }

    public ThreadGroup getThreadGroup() {
        return threadGroup;
    }
}
